<template>
  <div class="login-top">
    <van-nav-bar class="top" title="账号登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button square block type="info" native-type="submit">登 录</van-button>
      </div>
      <router-link to="/register" style="color: #666">还没有账号，去注册~</router-link>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit(value) {
      const data = await login(value)

      //保存token
      setToken(data.body.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    },
    onClickLeft() {
      this.$router.push('/home/homepage')
    }
  }
}
</script>

<style>
.top {
  background-color: #087 !important;
  font-size: 20px;
}

.login-top {
  font-size: 14px;
  text-align: center;
}
</style>
